/*
 * Copyright 2005-2013 biz-united. All rights reserved.
 * Support: http://www.biz-united.com.cn
 * License: http://www.biz-united.com.cn/?page_id=5&act=3
 */
package net.biz.entity;

import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import net.biz.util.ParamsUtil;

import org.hibernate.validator.constraints.NotEmpty;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * Entity - 订单项
 * 
 * @author BIZ Team
 * @version 3.0
 */
@Entity
@Table(name = "xx_order_item")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_order_item_sequence")
public class OrderItem extends BaseEntity {

	private static final long serialVersionUID = -4999926022604479334L;

	/** 商品编号 */
	private String sn;

	/** 商品名称 */
	private String name;

	/** 商品全称 */
	private String fullName;

	/** 商品价格 */
	private BigDecimal price;

	/** 商品重量 */
	private Integer weight;

	/** 商品缩略图 */
	private String thumbnail;

	/** 是否为赠品 */
	private Boolean isGift;

	/** 数量 */
	private Integer quantity;

	/** 已发货数量 */
	private Integer shippedQuantity;

	/** 已退货数量 */
	private Integer returnQuantity;

	/** 商品 */
	private Product product;

	/** 订单 */
	private Order order;

	/**
	 * 类型
	 */
	private Integer orderType;
//	/**
//	 * 方案主材
//	 */
//	private SchemeProduct schemeProduct;
	
	private Long schemeNameId;
	//方案
	private String schemeImage;
	private String schemeProductSn;
	private String schemeProductName;
	private String schemeProductBrand;
	private String schemeProductSpecificationValue;
	private BigDecimal schemeProductPrice;
	//门店
	private String shopSchemeImage;
	private String shopSchemeProductSn;
	private String shopSchemeProductName;
	private String shopSchemeProductBrand;
	private String shopSchemeProductSpecificationValue;
	private BigDecimal shopSchemeProductPrice;
	/** 初始数量 */
	private Integer iniQuantity; 
//	/**
//	 * 门店修改
//	 */
//	private SchemeProduct shopSchemeProduct;
	/**
	 * 差价
	 */
	private BigDecimal differencePrice;
	/**
	 * 是否门店录入数据
	 */
	private Boolean isShopAdd;
	
	/**门店下单奖励提成点*/
	private Double rewardPoint;
	/**
	 * 系数
	 */
	private BigDecimal coefficient;
	/**
	 * 获取商品编号
	 * 
	 * @return 商品编号
	 */
	@JsonProperty
	@NotEmpty
	@Column(nullable = false, updatable = false)
	public String getSn() {
		return sn;
	}

	/**
	 * 设置商品编号
	 * 
	 * @param sn
	 *            商品编号
	 */
	public void setSn(String sn) {
		this.sn = sn;
	}

	/**
	 * 获取商品名称
	 * 
	 * @return 商品名称
	 */
	@JsonProperty
	@Column(nullable = false, updatable = false)
	public String getName() {
		return name;
	}

	/**
	 * 设置商品名称
	 * 
	 * @param name
	 *            商品名称
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 获取商品全称
	 * 
	 * @return 商品全称
	 */
	@JsonProperty
	@Column(nullable = false, updatable = false)
	public String getFullName() {
		return fullName;
	}

	/**
	 * 设置商品全称
	 * 
	 * @param fullName
	 *            商品全称
	 */
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	/**
	 * 获取商品价格
	 * 
	 * @return 商品价格
	 */
	@JsonProperty
	@NotNull
	@Min(0)
	@Digits(integer = 12, fraction = 3)
	@Column(nullable = false, precision = 21, scale = 6)
	public BigDecimal getPrice() {
		return price;
	}

	/**
	 * 设置商品价格
	 * 
	 * @param price
	 *            商品价格
	 */
	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	/**
	 * 获取商品重量
	 * 
	 * @return 商品重量
	 */
	@JsonProperty
	@Column(updatable = false)
	public Integer getWeight() {
		return weight;
	}

	/**
	 * 设置商品重量
	 * 
	 * @param weight
	 *            商品重量
	 */
	public void setWeight(Integer weight) {
		this.weight = weight;
	}

	/**
	 * 获取商品缩略图
	 * 
	 * @return 商品缩略图
	 */
	@JsonProperty
	@Column(updatable = false)
	public String getThumbnail() {
		return thumbnail;
	}

	/**
	 * 设置商品缩略图
	 * 
	 * @param thumbnail
	 *            商品缩略图
	 */
	public void setThumbnail(String thumbnail) {
		this.thumbnail = thumbnail;
	}

	/**
	 * 获取是否为赠品
	 * 
	 * @return 是否为赠品
	 */
	@JsonProperty
	@Column(nullable = false, updatable = false)
	public Boolean getIsGift() {
		return isGift;
	}

	/**
	 * 设置是否为赠品
	 * 
	 * @param isGift
	 *            是否为赠品
	 */
	public void setIsGift(Boolean isGift) {
		this.isGift = isGift;
	}

	/**
	 * 获取数量
	 * 
	 * @return 数量
	 */
	@JsonProperty
	@NotNull
	@Min(0)
	@Max(10000)
	@Column(nullable = false)
	public Integer getQuantity() {
		return quantity;
	}

	/**
	 * 设置数量
	 * 
	 * @param quantity
	 *            数量
	 */
	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	/**
	 * 获取已发货数量
	 * 
	 * @return 已发货数量
	 */
	@Column(nullable = false)
	public Integer getShippedQuantity() {
		return shippedQuantity;
	}

	/**
	 * 设置已发货数量
	 * 
	 * @param shippedQuantity
	 *            已发货数量
	 */
	public void setShippedQuantity(Integer shippedQuantity) {
		this.shippedQuantity = shippedQuantity;
	}

	/**
	 * 获取已退货数量
	 * 
	 * @return 已退货数量
	 */
	@Column(nullable = false)
	public Integer getReturnQuantity() {
		return returnQuantity;
	}

	/**
	 * 设置已退货数量
	 * 
	 * @param returnQuantity
	 *            已退货数量
	 */
	public void setReturnQuantity(Integer returnQuantity) {
		this.returnQuantity = returnQuantity;
	}

	/**
	 * 获取商品
	 * 
	 * @return 商品
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	public Product getProduct() {
		return product;
	}

	/**
	 * 设置商品
	 * 
	 * @param product
	 *            商品
	 */
	public void setProduct(Product product) {
		this.product = product;
	}

	/**
	 * 获取订单
	 * 
	 * @return 订单
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "orders", nullable = false, updatable = false)
	public Order getOrder() {
		return order;
	}

	/**
	 * 设置订单
	 * 
	 * @param order
	 *            订单
	 */
	public void setOrder(Order order) {
		this.order = order;
	}

	/**
	 * 获取商品总重量
	 * 
	 * @return 商品总重量
	 */
	@JsonProperty
	@Transient
	public int getTotalWeight() {
		if (getWeight() != null && getQuantity() != null) {
			return getWeight() * getQuantity();
		} else {
			return 0;
		}
	}

	/**
	 * 获取小计
	 * 
	 * @return 小计
	 */
	@JsonProperty
	@Transient
	public BigDecimal getSubtotal() {
		if (getPrice() != null && getQuantity() != null) {
			BigDecimal temp=getPrice().multiply(new BigDecimal(getQuantity()));
			if(getCoefficient()!=null){
				if(null!=getDifferencePrice()){
					temp=getPrice().add(getDifferencePrice());
				}
				temp=temp.multiply(getCoefficient());
			}else{
				if(ParamsUtil.SCHEME_PRODUCT_CUST_STR.indexOf(getName())!=-1){
					if(getShopSchemeProductPrice()!=null){
						if(null!=getDifferencePrice()){
							
							//temp=temp.add(getDifferencePrice());
						}
						temp=getShopSchemeProductPrice().multiply(new BigDecimal(getQuantity()));
						//temp=temp.subtract(getSchemeProductPrice().multiply(new BigDecimal(getIniQuantity())));
					}
				}else{
					if(null!=getDifferencePrice()){
						temp=temp.add(getDifferencePrice());
					}
				}
			}
			return temp;
		} else {
			return new BigDecimal(0);
		}
	}

	public Integer getOrderType() {
		return orderType;
	}

	public void setOrderType(Integer orderType) {
		this.orderType = orderType;
	}
//	@JsonProperty
//	@ManyToOne(fetch = FetchType.LAZY)
//	@JoinColumn(name = "scheme_product", nullable = false, updatable = false)
//	public SchemeProduct getSchemeProduct() {
//		return schemeProduct;
//	}
//
//	public void setSchemeProduct(SchemeProduct schemeProduct) {
//		this.schemeProduct = schemeProduct;
//	}

//	@JsonProperty
//	@ManyToOne(fetch = FetchType.LAZY)
//	@JoinColumn(name = "shop_scheme_product", nullable = false, updatable = false)
//	public SchemeProduct getShopSchemeProduct() {
//		return shopSchemeProduct;
//	}
//
//	public void setShopSchemeProduct(SchemeProduct shopSchemeProduct) {
//		this.shopSchemeProduct = shopSchemeProduct;
//	}

	public Boolean getIsShopAdd() {
		return isShopAdd;
	}

	public void setIsShopAdd(Boolean isShopAdd) {
		this.isShopAdd = isShopAdd;
	}
	@JsonProperty
	public BigDecimal getDifferencePrice() {
		if(differencePrice==null){
			differencePrice=BigDecimal.ZERO;
		}
		return differencePrice.setScale(2,BigDecimal.ROUND_HALF_UP);
	}

	public void setDifferencePrice(BigDecimal differencePrice) {
		this.differencePrice = differencePrice;
	}

	public String getSchemeImage() {
		return schemeImage;
	}

	public void setSchemeImage(String schemeImage) {
		this.schemeImage = schemeImage;
	}

	public String getSchemeProductSn() {
		return schemeProductSn;
	}

	public void setSchemeProductSn(String schemeProductSn) {
		this.schemeProductSn = schemeProductSn;
	}

	public String getSchemeProductName() {
		return schemeProductName;
	}

	public void setSchemeProductName(String schemeProductName) {
		this.schemeProductName = schemeProductName;
	}

	public String getSchemeProductBrand() {
		return schemeProductBrand;
	}

	public void setSchemeProductBrand(String schemeProductBrand) {
		this.schemeProductBrand = schemeProductBrand;
	}

	public String getSchemeProductSpecificationValue() {
		return schemeProductSpecificationValue;
	}

	public void setSchemeProductSpecificationValue(String schemeProductSpecificationValue) {
		this.schemeProductSpecificationValue = schemeProductSpecificationValue;
	}

	public BigDecimal getSchemeProductPrice() {
		return schemeProductPrice;
	}

	public void setSchemeProductPrice(BigDecimal schemeProductPrice) {
		this.schemeProductPrice = schemeProductPrice;
	}

	public String getShopSchemeImage() {
		return shopSchemeImage;
	}

	public void setShopSchemeImage(String shopSchemeImage) {
		this.shopSchemeImage = shopSchemeImage;
	}

	public String getShopSchemeProductSn() {
		return shopSchemeProductSn;
	}

	public void setShopSchemeProductSn(String shopSchemeProductSn) {
		this.shopSchemeProductSn = shopSchemeProductSn;
	}

	public String getShopSchemeProductName() {
		return shopSchemeProductName;
	}

	public void setShopSchemeProductName(String shopSchemeProductName) {
		this.shopSchemeProductName = shopSchemeProductName;
	}

	public String getShopSchemeProductBrand() {
		return shopSchemeProductBrand;
	}

	public void setShopSchemeProductBrand(String shopSchemeProductBrand) {
		this.shopSchemeProductBrand = shopSchemeProductBrand;
	}

	public String getShopSchemeProductSpecificationValue() {
		return shopSchemeProductSpecificationValue;
	}

	public void setShopSchemeProductSpecificationValue(String shopSchemeProductSpecificationValue) {
		this.shopSchemeProductSpecificationValue = shopSchemeProductSpecificationValue;
	}

	public BigDecimal getShopSchemeProductPrice() {
		return shopSchemeProductPrice;
	}

	public void setShopSchemeProductPrice(BigDecimal shopSchemeProductPrice) {
		this.shopSchemeProductPrice = shopSchemeProductPrice;
	}

	public Long getSchemeNameId() {
		return schemeNameId;
	}

	public void setSchemeNameId(Long schemeNameId) {
		this.schemeNameId = schemeNameId;
	}

	public Double getRewardPoint() {
		return rewardPoint;
	}

	public void setRewardPoint(Double rewardPoint) {
		this.rewardPoint = rewardPoint;
	}

	/**
	 * 计算方案订单销项税金
	 */
	@Transient
	public BigDecimal getOutputTax() {
		//如果是方案
		if (getOrderType() != null && getOrderType() == ParamsUtil.CART_TYPE_NORMAL) {
			BigDecimal column1 = new BigDecimal(ParamsUtil.ORDER_REPORT_TAX_COLUMN_1).setScale(2,BigDecimal.ROUND_HALF_UP);
			BigDecimal column2 = new BigDecimal(ParamsUtil.ORDER_REPORT_TAX_COLUMN_2).setScale(2,BigDecimal.ROUND_HALF_UP);
			BigDecimal subtotal = getSubtotal().setScale(2,BigDecimal.ROUND_HALF_UP);
			BigDecimal divi = subtotal.divide(column1,2);
			BigDecimal tax = divi.multiply(column2).setScale(2,BigDecimal.ROUND_HALF_UP);
			return tax;
		}
		return null;
	}
	/***
	 * 计算订单调整后金额/订单总价的比例
	 * @return
	 */
	@JsonProperty
	@Transient
	public BigDecimal getProportions() {
		BigDecimal total = new BigDecimal("0");//订单总价
		BigDecimal result = BigDecimal.ZERO;
		for(OrderItem items : order.getOrderItems()){
			total=total.add(items.getSubtotal()).setScale(2,BigDecimal.ROUND_HALF_UP);
		}
		BigDecimal proportion = order.getAmount().divide(total,3);//比例
		return proportion;
	}
	/**
	 * 计算下单门店奖励
	 * @return
	 */
	@Transient
	public BigDecimal getShopReward() {
		if(getOrderType() == ParamsUtil.CART_TYPE_NORMAL){
			BigDecimal percent = new BigDecimal(100);
			BigDecimal result = BigDecimal.ZERO;
			BigDecimal proportion = getProportions();//比例
			result = result.add((getSubtotal().multiply(proportion)).multiply(new BigDecimal(getRewardPoint()).divide(new BigDecimal("100")))).setScale(2,BigDecimal.ROUND_HALF_UP);
			return result;
/*			if(getSubtotal() != null && getRewardPoint() != null){
				//订单行价-订单行价*（订单折扣价/订单总价）*提成点
				return getSubtotal().multiply(new BigDecimal(getRewardPoint())).divide(percent).setScale(2,BigDecimal.ROUND_HALF_UP);
			}*/
		}
		return BigDecimal.ZERO;
	}
	public BigDecimal getCoefficient() {
		return coefficient;
	}

	public void setCoefficient(BigDecimal coefficient) {
		this.coefficient = coefficient;
	}

	@JsonProperty
	public Integer getIniQuantity() {
		return iniQuantity;
	}

	public void setIniQuantity(Integer iniQuantity) {
		this.iniQuantity = iniQuantity;
	}


	
	
}